<?php

namespace app\common\logic;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\model\Collection;
use app\common\model\Service;
use app\common\model\ServiceMainBusiness;
use app\common\model\ServiceShop;
use app\common\model\ShopGoods;
use app\common\model\UsedCar;
use think\db\Query;

class CollectionLogic extends BaseLogic
{
    public $model;

    public function assignModel()
    {
        $this->model          = new Collection();
    }
    public function handleSearch(Query $query, array $params): Query
    {
        if (isset($params['type'])) {
            $query->where('service_type',$params['type']);
        }
        if (isset($params['member_id'])) {
            $query->where('member_id',$params['member_id']);
        }
        return $query;
    }

    /**
     * 收藏类目
     * @return array
     *
     */
    public function types()
    {
        $list = Service::getService('id,name')->toArray();
        array_unshift($list,['id'=>0,'name'=>'商品']);
        return $list;
    }

    /**
     * 收藏洗车列表
     * @param $params
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function xiche($params)
    {
        $params['member_id'] = MemberVo::instance()->getMemberId();
        $params['orderBy']   = 'createtime';
        $params['orderType'] = 'desc';
        $list = $this->getPageList($params)->each(function (&$item){
            $info = ShopGoods::get($item['main_id'],['goods','shop']);
//            $info['shop'] = $info['shop_backend'];
//            unset($info['shop_backend']);
            $item['goods_id'] = $info['goods_id'];
            $item['shop_id'] = $info['shop_id'];
            $item['cover_img'] = $info['goods']['cover_img'];
            $item['goods_name'] = $info['goods']['goods_name'];
            $item['shop_name'] = $info['shop']['shop_name'];
            $item['phone'] = $info['shop']['phone'];
            $item['longitude'] = $info['shop']['longitude'];
            $item['latitude'] = $info['shop']['latitude'];
            $item['is_collection'] = 1;
            $item['collection_id'] = $item['id'];
            $item['id'] = $info['id'];
            $price_info = ShopGoods::getPrice($info['market_price']);//价格
            $item['price'] = $price_info['price'];
            unset($item['createtime'],$item['member_id'],$item['series'],$item['models'],$item['member']);
            return $item;
        });
        return $list;
    }

    /**
     * 收藏 二手车列表
     * @param $params
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function used_car($params)
    {
        $params['member_id'] = MemberVo::instance()->getMemberId();
        $params['orderBy']   = 'createtime';
        $params['orderType'] = 'desc';
        $list = $this->getPageList($params)->each(function (&$item){
            $info = UsedCar::get($item['main_id'],['models','series','brand','shop','member']);
            $item['collection_id'] = $item['id'];
            $item['edit'] = 0;
            if ($info['type'] == 1 && $info['member_id'] == $item['member_id']){
                $item['edit'] = 1;
            }
            $item['id'] = $info['id'];
            $item['phone'] = $info['phone'];
            $item['cover_img'] = $info['cover_img'];
            $item['type'] = $info['type'];
            $item['shop_id'] = $info['shop_id'];
            $item['longitude'] = $info['longitude'];
            $item['latitude'] = $info['latitude'];
            if ($info['type'] == 2){
                $item['shop_name'] = $info['shop']['shop_name'];
            }else{
                $item['shop_name'] = $info['member']['member_name'];
            }
            $dif =  getMonthNum($info['car_age'],time());
            $str = '';
            if ($dif[1] >0){
                $str = $dif[1].'年';
            }
            if ($dif[2] >0){
                $str = $str.$dif[2].'月';
            }
            $item['car_age'] = $str;
            $item['mileage'] = $info['mileage'].'万公里';
            $item['is_collection'] = 1;
            $item['price'] = sprintf("%01.2f", $info['price']).'万元';
            $item['down_payment'] = $info['down_payment'] >0 ? sprintf("%01.2f", $info['down_payment']).'万元' : '';
            $item['car_name'] = $info['brand']['name'].$info['series']['name'].$info['models']['name'].$info['models']['year'];
            unset($item['createtime'],$item['member_id'],$item['brand'],$item['series'],$item['models'],$item['member']);
            return $item;
        });
        return $list;
    }

    public function service($params)
    {
        $params['member_id'] = MemberVo::instance()->getMemberId();
        $params['orderBy']   = 'createtime';
        $params['orderType'] = 'desc';
        $list = $this->getPageList($params)->each(function (&$item){
            $info = ServiceShop::get($item['main_id'],['province','city','districts','shop','member'])->toArray();
            $item['collection_id'] = $item['id'];
            $item['edit'] = 0;
            if ($info['type'] == 1 && $info['member_id'] == $item['member_id']){
                $item['edit'] = 1;
            }
            $item['id'] = $info['id'];
            $item['phone'] = $info['phone'];
            $item['shop_name'] = $info['name'];
            $item['shop_id'] = $info['shop_id'];
            $item['longitude'] = $info['longitude'];
            $item['latitude'] = $info['latitude'];
            $item['cover_img'] = $info['cover_img'];
            $item['type'] = $info['type'];
            $item['address'] = $info['province']['name'].$info['city']['name'].$info['districts']['name'].$info['address'];
            $item['main_business'] = ServiceMainBusiness::where(['id'=>['in',$info['main_business']]])->column('name');;
            $item['is_collection'] = 1;
            $item['distance'] = '';
            unset($item['createtime'],$item['member_id']);
            return $item;
        });
        return $list;
    }

    /**
     * 添加
     * @param $params
     * @return bool
     * @throws ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add($params)
    {
        //查看是否已收藏
        $info = $this->first(['service_type'=>$params['type'],'main_id'=>$params['id'],'member_id'=>MemberVo::instance()->getMemberId()]);
        if ($info)
            throw new ApiException('已收藏过，请勿重复收藏',404);
        Collection::create(['service_type'=>intval($params['type']),'main_id'=>intval($params['id']),'member_id'=>MemberVo::instance()->getMemberId()]);
        return true;
    }

    /**
     * 删除
     * @param $params
     * @return bool
     * @throws ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del($params)
    {
        //查看是否已收藏
        $info = $this->first(['id'=>$params['collection_id'],'member_id'=>MemberVo::instance()->getMemberId()]);
        if (!$info)
            throw new ApiException('没有该收藏记录',404);
        $info->delete();
        return true;
    }




}